/**
 * Package	: com.coship.adstorm.webservice.position.positioninfo
 * File		: PositionInfoServiceSkeleton.java
 * Company 	: Shenzhen Coship Electronics CO., LTD.
 * Version  : v1.0
 * Description 	: <Detail Description for package>
 *
 * Copyright (c) 2003-2009 Shenzhen Coship Electronics CO., LTD.
 * Rainbow Bldg.,North,Hi-Tech Industrial Park,Nanshan District,Shenzhen,China,P.C.518057.
 * All rights reserved.
 * The contents of this file are confidential and proprietary to COSHIP.
 * Copying is explicitly prohibited without the express permission of COSHIP.
 *
 * Create Date	: Apr 27, 2010
 * Create By    : 904120
 * History :$
 */
package com.coship.vasms.adstorm.webservice.position.positioninfo;

import org.apache.commons.lang.StringUtils;

import com.coship.vasms.adstorm.common.BeanHelper;
import com.coship.vasms.adstorm.common.log.JLogger;
import com.coship.vasms.adstorm.common.log.LoggerFactory;
import com.coship.vasms.adstorm.service.PositionInfoService;
import com.coship.vasms.adstorm.webservice.WebServiceConstant;
import com.coship.vasms.adstorm.webservice.WebServiceUtils;
import com.coship.vasms.adstorm.webservice.position.Response;
import com.coship.vasms.adstorm.webservice.position.positioninfo.vo.PositionInfoReq;

/**
 * 
 * File Name : PositionInfoServiceSkeleton.java
 * 
 * @Description : 广告位附加信息webservice接口实现类
 * @author 904120
 */
public class PositionInfoServiceSkeleton implements
		PositionInfoServiceSkeletonInterface {

	private static JLogger logger = LoggerFactory
			.getLogger(PositionInfoServiceSkeleton.class);
	private PositionInfoService service = (PositionInfoService) BeanHelper
			.getInstance().getBean("positionInfoService");

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.coship.adstorm.webservice.position.positioninfo.PositionInfoServiceSkeletonInterface#synPositionInfo(com.coship.adstorm.webservice.position.positioninfo.vo.PositionInfoReq)
	 */
	@Override
	public Response synPositionInfo(PositionInfoReq requset) {
		// TODO Auto-generated method stub
		logger.debug("请求广告位附加信息开始...");
		Response response = null;
		try {
			// 检查请求数据
			if (!checkReqData(requset)) {
				logger.debug("广告位附加信息同步请求数据校验不通过");
				response = WebServiceUtils.buildResponse(
						WebServiceConstant.RESPONSE_ERROR_PARAMETER,
						"数据不完整,同步失败");
				return response;
			}
			response = service.synPositionInfo(requset);

		} catch (Exception e) {
			logger.debug("同步广告位基本信息失败,cause:", e);
			response = WebServiceUtils.buildResponse(
					WebServiceConstant.RESPONSE_ERROR_SYSTEM, "系统发生异常");
		}
		if (response == null) {
			response = WebServiceUtils.buildResponse(
					WebServiceConstant.RESPONSE_ERROR_SYSTEM, "系统发生异常");
		}
		logger.debug("请求广告位附加信息结束");
		return response;
	}

	/**
	 * 
	 * Description : 校验数据
	 * 
	 * @param requset
	 * @return
	 * 
	 */
	private boolean checkReqData(PositionInfoReq requset) {
		if (requset == null) {
			return false;
		}
		// 代理类型
		if (!WebServiceConstant.POSITION_ANGENTTYPE_VAS
				.equalsIgnoreCase(requset.getAgentType())) {
			return false;
		}
		// 广告位附加信息代码不能为空
		if (StringUtils.isEmpty(requset.getPositionCode())) {
			return false;
		}
		// 广告位基本信息代码不能为空
		if (StringUtils.isEmpty(requset.getPositionBasicCode())) {
			return false;
		}
		// 广告位操作类型
		if (!WebServiceConstant.POSITIONINFO_OP_ADD.equalsIgnoreCase(requset
				.getOperation())
				&& !WebServiceConstant.POSITIONINFO_OP_DELETE
						.equalsIgnoreCase(requset.getOperation())
				&& !WebServiceConstant.POSITIONINFO_OP_MODIFY
						.equalsIgnoreCase(requset.getOperation())) {
			return false;
		}
		// 广告位附加信息类型
		/*
		 * 位置类型 1:VAS Portal 总入口页,2:VAS Portal 业务首页,3:VAS Portal 业务中特性页面, 4:VAS
		 * Portal 列表类页面,5:VAS Portal 产品内容详情页面 -->
		 */
		if (!WebServiceConstant.POSITIONINFO_POSITIONTYPE_VASENTER
				.equalsIgnoreCase(requset.getPositionType())
				&& !WebServiceConstant.POSITIONINFO_POSITIONTYPE_SERVICE
						.equalsIgnoreCase(requset.getPositionType())
				&& !WebServiceConstant.POSITIONINFO_POSITIONTYPE_FEATHURE
						.equalsIgnoreCase(requset.getPositionType())
				&& !WebServiceConstant.POSITIONINFO_POSITIONTYPE_COLUM
						.equalsIgnoreCase(requset.getPositionType())
				&& !WebServiceConstant.POSITIONINFO_POSITIONTYPE_CONTENT
						.equalsIgnoreCase(requset.getPositionType())) {
			return false;
		}
		// 不是1:VAS Portal 总入口页
		if (!WebServiceConstant.POSITIONINFO_POSITIONTYPE_VASENTER
				.equalsIgnoreCase(requset.getPositionType())) {
			// 如果是新增或者修改
			if (WebServiceConstant.POSITIONINFO_OP_ADD.equalsIgnoreCase(requset
					.getOperation())
					|| WebServiceConstant.POSITIONBASICINFO_OP_MODIFY
							.equalsIgnoreCase(requset.getOperation())) {
				// 搜索信息不能为空
				if (StringUtils.isEmpty(requset.getIndexContent())
						) {
					return false;
				}
				// 业务代码和业务名称不能为空
				if (StringUtils.isEmpty(requset.getServiceCode())
						|| StringUtils.isEmpty(requset.getServiceName())) {
					return false;
				}
				// 广告位类型是栏目,则业务代码和业务名称不能为空,栏目代码和栏目名称不能为空
				if (WebServiceConstant.POSITIONINFO_POSITIONTYPE_COLUM
						.equalsIgnoreCase(requset.getPositionType())) {
					if (StringUtils.isEmpty(requset.getColumnCode())
							|| StringUtils.isEmpty(requset.getColumnName())) {
						return false;
					}
				}
				// 广告位类型是文章和内容,则业务代码和业务名称不能为空,栏目代码和栏目名称不能为空,文章代码和文章内容不能为空
				if (WebServiceConstant.POSITIONINFO_POSITIONTYPE_CONTENT
						.equalsIgnoreCase(requset.getPositionType())) {
					if (StringUtils.isEmpty(requset.getColumnCode())
							|| StringUtils.isEmpty(requset.getColumnName())) {
						return false;
					}
					if (StringUtils.isEmpty(requset.getContentCode())
							|| StringUtils.isEmpty(requset.getContentName())) {
						return false;
					}
				}
			}
		}
		return true;
	}

}
